
<template>
  <div>
    <el-dialog
      ref="eldialog"
      v-bind="$attrs"
      @open="onOpen"
      @close="onClose"
      title="欢迎加入线性表学习软件平台"
      v-model="isVisiable"
    >
      <el-form
        ref="elForm"
        :model="formData"
        :rules="rules"
        size="medium"
        label-width="100px"
      >
        <el-form-item label="上传头像" prop="avatar " required>
          <el-upload
            ref="avatar "
            :file-list="avatarfileList"
            :action="avatarAction"
            :before-upload="avatarBeforeUpload"
            list-type="picture-card"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input
            v-model="formData.userName"
            placeholder="请输入用户名"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input
            v-model="formData.nickName"
            placeholder="请输入用户名"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="formData.password"
            placeholder="请输入密码"
            clearable
            show-password
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="phonenumber">
          <el-input
            v-model="formData.phonenumber"
            placeholder="请输入手机号码"
            clearable
            :style="{ width: '100%' }"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input
            v-model="formData.email"
            placeholder="请输入邮箱"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="学校" prop="school">
          <el-input
            v-model="formData.school"
            placeholder="请输入学校"
            clearable
            :style="{ width: '100%' }"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <el-footer>
        <el-button @click="isVisiable = false">取消</el-button>
        <el-button type="primary" @click="handelConfirm">注册</el-button>
      </el-footer>
    </el-dialog>
  </div>
</template>
<script>
import { ref } from 'vue'
import { register } from '../api/login'

export default {

  inheritAttrs: false,
  components: {},
  props: [
    'isVisiable'
  ],
  data () {
    return {
      formData: {
        avatar: null,
        userName: undefined,
        nickName: undefined,
        password: undefined,
        phonenumber: undefined,
        email: undefined,
        school: undefined,
      },
      rules: {
        avatar: [{
          required: false
        }],
        userName: [{
          required: true,
          message: '请输入用户名',
          trigger: 'blur'
        }],
        nickName: [{
          required: true,
          message: '请输入昵称',
          trigger: 'blur'
        }],
        password: [{
          required: true,
          message: '请输入密码',
          trigger: 'blur'
        }],
        phonenumber: [{
          required: true,
          message: '请输入手机号码',
          trigger: 'blur'
        }],
        email: [{
          required: true,
          message: '请输入邮箱',
          trigger: 'blur'
        }],
        school: [],
      },
      avatarAction: 'https://jsonplaceholder.typicode.com/posts/',
      avatarfileList: [],

    }
  },
  computed: {},
  watch: {},
  created () { },
  mounted () { },
  methods: {
    onOpen () { },
    onClose () {
      this.$refs['elForm'].resetFields()
      location.reload()
    },
    close () {
      this.$emit('update:visible', false)

    },
    handelConfirm () {
      this.$refs['elForm'].validate(valid => {
        if (!valid) return
        console.log(this.formData)
        register(this.formData)
        this.close()
      })
    },
    avatarBeforeUpload (file) {
      let isRightSize = file.size / 1024 / 1024 < 2
      if (!isRightSize) {
        this.$message.error('文件大小超过 2MB')
      }
      return isRightSize
    },
  }
}

</script>
<style>
.el-upload__tip {
  line-height: 1.2;
}
</style>
